home *** CD-ROM | disk | FTP | other *** search
- Path: news.iadfw.net!usenet
- From: alpet@airmail.net (Adam Peterson)
- Newsgroups: comp.lang.c
- Subject: Re: How do I modify a character string that's declared as an array of char?
- Date: Fri, 23 Feb 1996 05:14:36 GMT
- Organization: customer of Internet America
- Message-ID: <4gjbjh$o0h@news-f.iadfw.net>
- References: <4gj2nl$840@mirzam.usc.edu>
- NNTP-Posting-Host: dal24-12.ppp.iadfw.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- awawda@mirzam.usc.edu (Abu A. Wawda) wrote:
-
- >How do I write a function that can modify a string that was declared
- >as an array of characters?
-
- >for example:
-
- >myfunction(char **s)
- >{
- > // modify string s
- >}
-
- >main()
- >{
- > char mydata[50];
-
- > strcpy(mydata,"test string");
- > myfunction(&mydata);
- >}
-
- >this does not seem to work! it works fine if i pass i declare mydata
- >as a pointer to a character like this (or dynamically allocate the
- >string using a pointer) and pass the address of the pointer:
-
- >the problem with this is i need to write a function that will modify a
- >string created with an array of characters. i can't figure out how do
- >to this. please help. thanks!
-
- void myfunction(char *s)
- {
- strcat(s,"more stuff"); // modify string s
- }
-
- void main()
- {
- char mydata[50];
-
- strcpy(mydata,"test string");
- printf(mydata);
- myfunction(mydata);
- printf(mydata);
- }
-
- The above does what I think you want to do. When you call
- 'myfunction', the '&' was not neccessary by definition, then *in*
- 'myfunction', you were dropping two levels of redirection down.
-
- Adam
-
-
-